#ifndef CIRCUITSYSTEM_H
#define CIRCUITSYSTEM_H

#include <QObject>
#include <QGraphicsItem>
#include <QGraphicsLineItem>
#include <QGraphicsScene>
#include <QPen>
#include <QLineF>
#include <QRectF>
#include <QPointF>
#include <QList>
#include "LightBulbItem.h"
#include "PowerSourceItem.h"
#include "SwitchItem.h"
#include "ConnectionLine.h"
#include "MyGraphicsScene.h"
#include <QFormLayout>
#include <QObject>  // 确保包含这个头文件
#include <QComboBox>
#include <vector>
using std::vector;
// 电路系统类
class CircuitSystem : public QObject {
    Q_OBJECT

public:
    CircuitSystem(MyGraphicsScene *scene)
        : m_scene(scene), lightBulbCount('a'), powerSourceCount(1), switchCount(1) {
    }

    ~CircuitSystem() {
        // 删除所有连接线
        qDeleteAll(m_connections);
    }

    // 添加一个新组件
    void addComponent(QGraphicsItem *component) {
        m_components.append(component);
        m_scene->addItem(component);  // 将元件添加到场景

        // 如果当前组件是 LightBulbItem，则设置标签为单个字符（从 'a' 开始，每次递增）
        if (auto lightBulb = dynamic_cast<LightBulbItem*>(component)) {
            lightBulb->setLabel(QString(1, lightBulbCount++)+"灯泡");
        } else if (auto powerSource = dynamic_cast<PowerSourceItem*>(component)) {
            powerSource->setLabel(QString("电源%1").arg(powerSourceCount++));
        } else if (auto switchItem = dynamic_cast<SwitchItem*>(component)) {
            switchItem->setLabel(QString::number(switchCount++)+"开关");
        }

    }
    void connectComponents(QGraphicsItem* component, QGraphicsItem* existingComponent, QGraphicsScene* scene, int thickness) {
        if (component && existingComponent) {
            ConnectionLine* c = new ConnectionLine(component, existingComponent, scene, thickness);
            m_connections.append(c);
        }
        ElectricCurrent();
    }
    void deleteConnectionLine(QGraphicsItem* component1, QGraphicsItem* component2, QGraphicsScene* scene)
    {
        for (ConnectionLine *line : m_connections) {

            qDebug()<<"循环";
            if (!line) continue;

            QGraphicsItem *endItem = line->getEndItem();
            QGraphicsItem *startItem = line->getStartItem();

            // 检查 endItem 和 startItem 是否有效
            if ((startItem==component1&&endItem==component2)||(startItem==component2&&endItem==component1)) {

                // 移除连接线
                m_scene->removeItem(line);  // 从场景中移除连接线
                // Find the index of the pointer in the QList
                int index = m_connections.indexOf(line);

                // Check if the index is valid (i.e., the element is found)
                if (index != -1) {
                    // Remove the element at the found index
                    m_connections.removeAt(index);
                } else {
                    // Handle the case where the line was not found in the list
                    qDebug() << "ConnectionLine not found in the list.";
                }
                delete line;  // 删除连接线
            }
        }
        ElectricCurrent();
    }
    void removeComponent(QGraphicsItem *component) {


        if (!component) {
            qDebug() << "传入的组件为空";
            return;
        }

        qDebug() << "信号接受，准备删除组件" << component;

        // 移除与该组件相关的所有连接
        for (int i = m_connections.size() - 1; i >= 0; --i) {
            ConnectionLine *line = m_connections[i];

            // 确保连接线存在
            if (!line) {
                qDebug() << "跳过空连接线";
                continue;
            }

            // 判断连接线是否与组件相关联
            if (line->getStartItem() == component || line->getEndItem() == component) {
                qDebug() << "移除连接线: " << line;

                // 移除连接线
                m_scene->removeItem(line);  // 从场景中移除连接线
                m_connections.removeAt(i);  // 从连接列表中移除
                delete line;  // 删除连接线
            }
        }

        // 从场景中移除组件
        int index = m_components.indexOf(component);
        if (index != -1) {
            qDebug() << "移除组件: " << component;
            m_components.removeAt(index);  // 从组件列表中移除
            m_scene->removeItem(component);  // 从场景中移除组件
            delete component;  // 删除组件
        } else {
            qDebug() << "未找到组件，无法删除";
        }

        // 重新编号所有剩余组件
        updateLabels();

        // 刷新场景
        qDebug() << "刷新场景";
        m_scene->update();  // 强制刷新场景视图
        m_scene->invalidate();  // 强制重绘场景视图

        ElectricCurrent();

    }

    void checkSwitchConnections(QGraphicsItem *item ,QComboBox *comboBox,QComboBox *comboBox_2,QComboBox *comboBox_3,QComboBox *comboBox_4,QComboBox *comboBox_5,QComboBox *comboBox_6) {

        if (!item) {
            qDebug() << "传入的组件为空";
            return;
        }

        qDebug() << "传入的组件";
        if (auto switchItem = dynamic_cast<SwitchItem*>(item)) {
            vector<QString> labels;

            for (QGraphicsItem *component : m_components) {
                if(item!=component)
                {
                    if (auto *lightBulb = dynamic_cast<LightBulbItem *>(component)) {
                        labels.push_back(lightBulb->getLabel());
                        qDebug()<<lightBulb->getLabel();
                    }
                    else if (auto *powerSource = dynamic_cast<PowerSourceItem *>(component)) {
                        labels.push_back(powerSource->getLabel());
                        qDebug()<<powerSource->getLabel();
                    }
                    else if (auto *switchItem = dynamic_cast<SwitchItem *>(component)) {
                        QString label = switchItem->getLabel();
                        labels.push_back(label + "1端");
                        labels.push_back(label + "2端");
                        qDebug()<<switchItem->getLabel();
                    }
                }
            }

            int x = 1;
            int x1 = 1;
            // 对 comboBox 进行处理
            QString currentText = comboBox->currentText();
            comboBox->clear();
            if (!currentText.isEmpty()) {
                comboBox->addItem(currentText);
                comboBox->setCurrentText(currentText);
                // 查找目标字符串
                auto it = std::find(labels.begin(), labels.end(), currentText);
                if (it != labels.end()) {
                    qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                    labels.erase(it);  // 将匹配的元素从 vector 中删除
                }
            }

            // 对 comboBox_2 进行处理
            currentText = comboBox_2->currentText();
            comboBox_2->clear();
            if (!currentText.isEmpty()) {
                comboBox_2->addItem(currentText);
                comboBox_2->setCurrentText(currentText);
                // 查找目标字符串
                auto it = std::find(labels.begin(), labels.end(), currentText);
                if (it != labels.end()) {
                    qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                    labels.erase(it);  // 将匹配的元素从 vector 中删除
                }
            }

            // 对 comboBox_3 进行处理
            currentText = comboBox_3->currentText();
            comboBox_3->clear();
            if (!currentText.isEmpty()) {
                comboBox_3->addItem(currentText);
                comboBox_3->setCurrentText(currentText);
                // 查找目标字符串
                auto it = std::find(labels.begin(), labels.end(), currentText);
                if (it != labels.end()) {
                    qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                    labels.erase(it);  // 将匹配的元素从 vector 中删除
                }
            }

            // 对 comboBox_4 进行处理
            currentText = comboBox_4->currentText();
            comboBox_4->clear();
            if (!currentText.isEmpty()) {
                comboBox_4->addItem(currentText);
                comboBox_4->setCurrentText(currentText);
                // 查找目标字符串
                auto it = std::find(labels.begin(), labels.end(), currentText);
                if (it != labels.end()) {
                    qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                    labels.erase(it);  // 将匹配的元素从 vector 中删除
                }
            }

            // 对 comboBox_5 进行处理
            currentText = comboBox_5->currentText();
            comboBox_5->clear();
            if (!currentText.isEmpty()) {
                comboBox_5->addItem(currentText);
                comboBox_5->setCurrentText(currentText);
                // 查找目标字符串
                auto it = std::find(labels.begin(), labels.end(), currentText);
                if (it != labels.end()) {
                    qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                    labels.erase(it);  // 将匹配的元素从 vector 中删除
                }
            }

            // 对 comboBox_6 进行处理
            currentText = comboBox_6->currentText();
            comboBox_6->clear();
            if (!currentText.isEmpty()) {
                comboBox_6->addItem(currentText);
                comboBox_6->setCurrentText(currentText);
                // 查找目标字符串
                auto it = std::find(labels.begin(), labels.end(), currentText);
                if (it != labels.end()) {
                    qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                    labels.erase(it);  // 将匹配的元素从 vector 中删除
                }
            }


            // 判断连接链是否为空
            if (m_connections.isEmpty()) {
                goto ks;
                return;
            }
            for (ConnectionLine *line : m_connections) {

                qDebug()<<"循环";
                if (!line) continue;

                QGraphicsItem *endItem = line->getEndItem();
                QGraphicsItem *startItem = line->getStartItem();

                // 检查 endItem 和 startItem 是否有效
                if (startItem==item||endItem==item) {

                    if(line->getconnectionType()==1)
                    {
                        if(x==1)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }
                            target+="1端";

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                // 设置组合框的当前文本
                                comboBox->addItem(target);
                                comboBox->setCurrentText(target);
                                qDebug()<<target;
                                x++;
                            }

                        }
                        else if(x==2)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }
                            target+="1端";

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                // 设置组合框的当前文本
                                comboBox_2->addItem(target);
                                comboBox_2->setCurrentText(target);
                                qDebug()<<target;
                                x++;
                            }

                        }
                        else if(x==3)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }
                            target+="1端";

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                // 设置组合框的当前文本
                                comboBox_3->addItem(target);
                                comboBox_3->setCurrentText(target);
                                qDebug()<<target;
                                x++;
                            }

                        }
                    }
                    else if(line->getconnectionType()==2)
                    {
                        if(x==1)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                // 设置组合框的当前文本
                                comboBox->addItem(target);
                                comboBox->setCurrentText(target);
                                qDebug()<<target;
                                x++;
                            }

                        }
                        else if(x==2)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除
                                // 设置组合框的当前文本
                                comboBox_2->addItem(target);
                                comboBox_2->setCurrentText(target);
                                qDebug()<<target;
                                x++;
                            }

                        }
                        else if(x==3)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除
                                // 设置组合框的当前文本
                                comboBox_3->addItem(target);
                                comboBox_3->setCurrentText(target);
                                qDebug()<<target;
                                x++;
                            }
                        }
                    }
                    else if(line->getconnectionType()==3)
                    {
                        if(x==1)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }
                            target+="2端";

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除
                                // 设置组合框的当前文本
                                comboBox->addItem(target);
                                comboBox->setCurrentText(target);
                                qDebug()<<target;
                                x++;
                            }

                        }
                        else if(x==2)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                target+="2端";
                                // 设置组合框的当前文本
                                comboBox_2->addItem(target);
                                comboBox_2->setCurrentText(target);
                                qDebug()<<target;
                                x++;
                            }

                        }
                        else if(x==3)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }
                            target+="2端";

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                // 设置组合框的当前文本
                                comboBox_3->addItem(target);
                                comboBox_3->setCurrentText(target);
                                qDebug()<<target;
                                x++;
                            }

                        }
                    }
                    else if(line->getconnectionType()==4)
                    {
                        if(x1==1)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }
                            target+="1端";


                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                // 设置组合框的当前文本
                                comboBox_4->addItem(target);
                                comboBox_4->setCurrentText(target);
                                qDebug()<<target;
                                x1++;
                            }
                        }
                        else if(x1==2)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }
                            target+="1端";

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                // 设置组合框的当前文本
                                comboBox_5->addItem(target);
                                comboBox_5->setCurrentText(target);
                                qDebug()<<target;
                                x1++;
                            }
                        }
                        else if(x1==3)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }
                            target+="1端";

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                // 设置组合框的当前文本
                                comboBox_6->addItem(target);
                                comboBox_6->setCurrentText(target);
                                qDebug()<<target;
                                x1++;
                            }
                        }
                    }
                    else if(line->getconnectionType()==5)
                    {
                        if(x1==1)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                // 设置组合框的当前文本
                                comboBox_4->addItem(target);
                                comboBox_4->setCurrentText(target);
                                qDebug()<<target;
                                x1++;
                            }

                        }
                        else if(x1==2)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除
                                // 设置组合框的当前文本
                                comboBox_5->addItem(target);
                                comboBox_5->setCurrentText(target);
                                qDebug()<<target;
                                x1++;
                            }

                        }
                        else if(x1==3)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除
                                // 设置组合框的当前文本
                                comboBox_6->addItem(target);
                                comboBox_6->setCurrentText(target);
                                qDebug()<<target;
                                x1++;
                            }
                        }
                    }
                    else if(line->getconnectionType()==6)
                    {
                        if(x1==1)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }
                            target+="2端";

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除
                                // 设置组合框的当前文本
                                comboBox_4->addItem(target);
                                comboBox_4->setCurrentText(target);
                                qDebug()<<target;
                                x1++;
                            }

                        }
                        else if(x1==2)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                target+="2端";
                                // 设置组合框的当前文本
                                comboBox_5->addItem(target);
                                comboBox_5->setCurrentText(target);
                                qDebug()<<target;
                                x1++;
                            }

                        }
                        else if(x1==3)
                        {
                            QString target;
                            if (item == startItem) {
                                target = dynamic_cast<SwitchItem *>(endItem)->getLabel();
                            } else {
                                target = dynamic_cast<SwitchItem *>(startItem)->getLabel();
                            }
                            target+="2端";

                            // 查找目标字符串
                            auto it = std::find(labels.begin(), labels.end(), target);
                            if (it != labels.end()) {
                                qDebug() << "找到了匹配的标签:" << *it << "，正在删除...";
                                labels.erase(it);  // 将匹配的元素从 vector 中删除

                                // 设置组合框的当前文本
                                comboBox_6->addItem(target);
                                comboBox_6->setCurrentText(target);
                                qDebug()<<target;
                                x1++;
                            }
                        }
                    }

                }
            }
            qDebug()<<"待选项";
ks:
            // 将 labels 中的元素添加到 comboBox、comboBox_2 和 comboBox_3
            for (const QString& label : labels) {
                qDebug()<<label;
                comboBox->addItem(label);
                comboBox_2->addItem(label);
                comboBox_3->addItem(label);
            }

            if (x == 3) {
                comboBox_3->setCurrentIndex(-1);
            }
            else if (x == 2) {
                comboBox_3->setCurrentIndex(-1);// 禁用 comboBox_3
                comboBox_2->setCurrentIndex(-1);  // 启用 comboBox_2
            }
            else if (x == 1) {
                comboBox_3->setCurrentIndex(-1); // 禁用 comboBox_3
                comboBox_2->setCurrentIndex(-1);// 禁用 comboBox_2
                comboBox->setCurrentIndex(-1);    // 启用 comboBox
            }
            // 将 labels 中的元素添加到 comboBox、comboBox_2 和 comboBox_3
            for (const QString& label : labels) {
                qDebug()<<label;
                comboBox_4->addItem(label);
                comboBox_5->addItem(label);
                comboBox_6->addItem(label);
            }

            if (x1== 3) {
                comboBox_6->setCurrentIndex(-1);
            }
            else if (x1== 2) {
                comboBox_6->setCurrentIndex(-1);// 禁用 comboBox_3
                comboBox_5->setCurrentIndex(-1);  // 启用 comboBox_2
            }
            else if (x1 == 1) {
                comboBox_4->setCurrentIndex(-1); // 禁用 comboBox_3
                comboBox_5->setCurrentIndex(-1);// 禁用 comboBox_2
                comboBox_6->setCurrentIndex(-1);    // 启用 comboBox
            }
            qDebug()<<x;
        }
    }
    //电流
    void ElectricCurrent()
    {
        qDebug()<<"电流状态更新";
        for (QGraphicsItem *component :m_components)
        {
            if (auto *lightBulbItem = dynamic_cast<LightBulbItem *>(component)) {

                lightBulbItem->setlight(false);
            }
        }
        for (QGraphicsItem *component :m_components)
        {
            if (auto *powerSource = dynamic_cast<PowerSourceItem *>(component)) {

                QSet<QGraphicsItem*> visitedItems;
                dfs(powerSource, 0, &visitedItems);  // 从某个组件开始 DFS，初始深度为 0
            }
        }
    }
    void dfs(QGraphicsItem *component, int depth, QSet<QGraphicsItem*>* visitedItems)
    {
        if (!component || visitedItems->contains(component)) {
            return;  // 如果组件为空或已经访问过，直接返回
        }

        // 标记当前组件为已访问
        visitedItems->insert(component);

        // 输出当前组件的层次（深度）信息
        qDebug() << "Component at depth" << depth;

        // 遍历所有连接线
        for (ConnectionLine *line : m_connections) {
            qDebug() << "循环";
            if (!line)
                continue;

            QGraphicsItem *endItem = line->getEndItem();
            QGraphicsItem *startItem = line->getStartItem();

            // 如果当前组件是连接线的起点
            if (startItem == component) {
                if (auto lightBulb = dynamic_cast<LightBulbItem*>(endItem)) {
                    qDebug()<<lightBulb->getLabel();
                    lightBulb->setlight(true);
                    dfs(lightBulb, depth + 1, visitedItems);  // 递归调用，层次加 1
                } else if (auto switchItem = dynamic_cast<SwitchItem*>(endItem)) {
                    if (switchItem->getLight() == true) {
                        qDebug()<<switchItem->getLabel();
                        dfs(switchItem, depth + 1, visitedItems);  // 递归调用，层次加 1
                    }
                } else {
                    dfs(endItem, depth + 1, visitedItems);  // 递归调用，层次加 1
                }
            }

            // 如果当前组件是连接线的终点
            if (endItem == component) {
                if (auto lightBulb = dynamic_cast<LightBulbItem*>(startItem)) {
                    qDebug()<<lightBulb->getLabel();
                    lightBulb->setlight(true);
                    dfs(lightBulb, depth + 1, visitedItems);  // 递归调用，层次加 1
                } else if (auto switchItem = dynamic_cast<SwitchItem*>(startItem)) {
                    if (switchItem->getLight() == true) {
                        qDebug()<<switchItem->getLabel();
                        dfs(switchItem, depth + 1, visitedItems);  // 递归调用，层次加 1
                    }
                } else {
                    dfs(startItem, depth + 1, visitedItems);  // 递归调用，层次加 1
                }
            }
        }
    }

private:
    void updateLabels() {
        int i = 1;      // 用于给 SwitchItem 编号（数字，从1开始）
        int j = 1;      // 用于给 PowerSourceItem 编号（数字，从1开始）
        char ch = 'a';  // 用于给 LightBulbItem 编号（字母，从 'a' 开始）

        for (QGraphicsItem *component : m_components) {
            // 如果当前组件是 LightBulbItem，则设置标签为单个字符（从 'a' 开始，每次递增）
            if (auto lightBulb = dynamic_cast<LightBulbItem*>(component)) {
                lightBulb->setLabel(QString(1, ch++));
            }
            // 如果当前组件是 PowerSourceItem，则设置标签为 “电源”+序号，例如 "电源1", "电源2"…
            else if (auto powerSource = dynamic_cast<PowerSourceItem*>(component)) {
                powerSource->setLabel(QString("电源%1").arg(j++));
            }
            // 如果当前组件是 SwitchItem，则设置标签为数字字符串，例如 "1", "2", "3"…
            else if (auto switchItem = dynamic_cast<SwitchItem*>(component)) {
                switchItem->setLabel(QString::number(i++));
            }
        }
    }


    MyGraphicsScene *m_scene;  // 场景指针
    QList<QGraphicsItem *> m_components;  // 存储所有元件
    QList<ConnectionLine *> m_connections;  // 存储连接线

    char lightBulbCount;  // LightBulbItem的计数器
    int powerSourceCount;  // PowerSourceItem的计数器
    int switchCount;  // SwitchItem的计数器
};

#endif // CIRCUITSYSTEM_H
